import 'package:flutter/material.dart';
import 'package:flutter_comm_base_framework/core/select_model.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RadioButtonGroup extends StatefulWidget {

  List<SelectModel> items;
  bool isWarp = false;
  double itemMinWidth = 10;
  int lineItemCount = 3;


  RadioButtonGroup(this.items,{this.itemMinWidth});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RadioButtonGroupState();
  }

}

class RadioButtonGroupState extends State<RadioButtonGroup>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return widget.isWarp? Wrap(
        runSpacing:-10,
        spacing:5,
        children: widget.items.map(
                (SelectModel model) =>
                    RadioItem("${model.name}",isSelect: model.isSelect,constraints: BoxConstraints(minWidth: widget.itemMinWidth),
                      onSelectChanged: (bool selected){
                      selectItem(model);
                      },)
        ).toList()):
    Container(height: (widget.items.length+widget.lineItemCount-1)/widget.lineItemCount*40,
      child: GridView.builder(

          physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: widget.lineItemCount, //每行个数
            mainAxisSpacing: 0.0, //主轴(竖直)方向间距
            crossAxisSpacing: 0.0, //纵轴(水平)方向间距
            childAspectRatio: ScreenUtil.screenWidth/widget.lineItemCount/40 //纵轴缩放比例
        ),
        itemCount: widget.items.length,
        itemBuilder: (BuildContext context, int index) {
          SelectModel model = widget.items[index];
          return Container( margin: EdgeInsets.all(2), height: 40,child: RadioItem("${model.name}",isSelect: model.isSelect,constraints: BoxConstraints(minWidth: widget.itemMinWidth),
            onSelectChanged: (bool selected) {
            selectItem(model);
          },),);
        }),)
    ;
  }


  void selectItem(SelectModel item){

    List<SelectModel> items = widget.items;
    for(int i=0;i<items.length;i++){
      SelectModel itemi = items[i];
      itemi.isSelect = itemi==item;
    }
    setState(() {

    });
  }
}

class RadioItem extends StatefulWidget {
  bool isSelect = false;
  String title;
  Function(bool) onSelectChanged;
  BoxConstraints constraints;

  RadioItem(this.title, {this.isSelect = false,this.onSelectChanged,this.constraints});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RadioItemState();
  }
}

class RadioItemState extends State<RadioItem> {
  get children => null;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    Color bgColor;
    TextStyle titleStyle;
    if(widget.isSelect){
      bgColor = Color(0xff477bfc);
      titleStyle = TextStyle(color: Color(0xffffffff));
    }else{
      bgColor = Color(0xffedeeef);
      titleStyle = TextStyle(color: Color(0xff333333));
    }
    return
      GestureDetector(
        child:Container(
          //constraints: widget.constraints,
            child: Row(
              children:<Widget>[
                Container(
                  padding: EdgeInsets.all(7),
                  margin: EdgeInsets.only(right: 5),
                  width: 14,
                  height: 14,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    border: Border.all(color: Colors.grey, width: 1.0),
                    color: bgColor,
                  ),
                ),
                Expanded(flex: 1,child: Text(widget.title),),
              ] )),onTap: (){
        setState(() {
          widget.isSelect = !widget.isSelect;
          if(widget.onSelectChanged != null){
            widget.onSelectChanged(widget.isSelect);
          }
        });
      },);

    GestureDetector(
      child:Container(
          //constraints: widget.constraints,
      child: Chip(
      backgroundColor: Colors.transparent,
      avatar: Container(
        //child: Text(widget.title,style: titleStyle,textAlign: TextAlign.center,),
        padding: EdgeInsets.all(5),
        width: 10,
        height: 10,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: bgColor,
        ),
      ),label: Text(widget.title),)),onTap: (){
      setState(() {
        widget.isSelect = !widget.isSelect;
        if(widget.onSelectChanged != null){
          widget.onSelectChanged(widget.isSelect);
        }
      });
    },);
  }

}
